package com.howbuy.tools.stringconvert;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;

import com.howbuy.base.ToolBase;

public class StringConvert {
	private static String path = "SQL";
	private static String returnPath = "SQL";
	// 不送的话默认为fileName的txt格式
	private static String returnFileName = "";
	
	public static void main(String[] args) throws IOException {
		// 输入流
				File[] fileList = ToolBase.readFile(path, null);
				
				getSql(fileList);
	}
	
	
	private static void getSql(File[] fileList) throws IOException {
		StringBuffer paramSource = getSQLForDevelopment(fileList[0]);
		StringBuffer sfSource = getSQLForDevelopment(fileList[1]);
		String source = sfSource.toString();
		String str = paramSource.toString();
		str = str.replace("^\\([a-zA-Z]\\)$", "");
		str = str.replace("(String)", "");
		str = str.replace("(Long)", "");
		str = str.replace("(Double)", "");
		str = str.replace("(BigDecimal)", "");
		str = str.replace("(Timestamp)", "");
		String[] param = str.split(",");
		
		String res=convertString(source,param);
		System.out.println(res);
	}


	private static StringBuffer getSQLForDevelopment(File file) throws IOException {
		if (file.length() == 0) {
			System.out.println("没有读到文件");
		}
		FileInputStream fis = new FileInputStream(file);
		@SuppressWarnings("resource")
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis,ToolBase.CHARSET));
		String line = null;
		StringBuffer sf = new StringBuffer();
		//按行处理文件
		while ((line = bufferedReader.readLine()) != null) {
			if (line.contains("Parameters:")) {
				line=line.substring(line.indexOf("Parameters:")+11);
			}else if(line.contains("Preparing:")) {
				line=line.substring(line.indexOf("Preparing:")+10);
			}
			sf.append(line);
			}
		
		return sf;
	}


	static String convertString(String source,String[] str){
		String[] strSource=source.split("[?]");
		StringBuffer sf = new StringBuffer();
		for(int i=0;i<strSource.length;i++){
			sf.append(strSource[i]);
			if(i<strSource.length-1){
				sf.append("'");
				sf.append(str[i].trim());
				sf.append("'");
			}
		}
		return sf.toString();
	}
}
